<@t.html>
<link rel="stylesheet" href="public/static/ztree/css/zTreeStyle/zTreeStyle.css">
<@t.container title="栏目管理" >
					<div class="row">
                    	<div class="col-md-2">
           					<div class="well well-sm">
                                <div class="zcate_treeBackground left">
                                    <ul id="cate_tree" class="ztree"></ul>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-10">
                        <!--button--area-->
                        <@t.toolbar id="category_toolbar">
                        	 <@t.btn_b name="保存"  icon="save" onclick="$('#add_form').submit();"/>
                        	 <@t.btn_b name="删除"  icon="trash" onclick="deleteCategory();"/>
                        	 <@t.btn_b id="addTopBtn" name="创建顶级栏目"  icon="plus" onclick="$('#add_form').submit();"/>
                        	 <@t.btn_b name="创建子栏目"  icon="plus" onclick="addChild();"/>
                        </@t.toolbar>
                        	<!--单个按钮信息-->
                        	<@t.form id="add_form">
                        		<@t.legend title="基本信息"/>
								<input type="hidden" name="pid" id="pid" value="${bean.pid!'0'}">
								<@t.text title="栏目ID" readOnly="true" id="id" value="${bean.id!''}" />   
								<@t.select title="状态"  datas={"1":"启用","0":"停用"} id="state" defaultValue="${bean.state!''}" />   
								<@t.text title="栏目名称" id="name" value="${bean.name!''}" />   
								<@t.text title="栏目别名" id="slug" value="${bean.slug!''}" />   
								<@t.text title="所在目录" id="folder" value="${bean.folder!''}" />   
								<@t.select title="内容类型" id="type" datas={"1":"文章","2":"图片","3":"文件","4":"音频","5":"视频","6":"专题"} defaultValue="${bean.type!''}" />   
								<@t.textarea title="栏目描述" id="desc_text" value="${bean.desc_text!''}" />   
								<@t.text title="顺序" id="seq" value="${bean.seq!''}" /> 
								<@t.legend title="PC模板属性"/>
								<@t.text title="首页模板" id="pc_index_template" value="${bean.pc_index_template!''}" /> 
								<@t.text title="列表页模板" id="pc_list_template" value="${bean.pc_list_template!''}" /> 
								<@t.text title="详细页模板" id="pc_detail_template" value="${bean.pc_detail_template!''}" /> 
								<@t.text title="列表页显示文档数" id="pc_page_size" value="${bean.pc_page_size!'20'}" /> 
								<@t.legend title="SEO属性设置"/>
								<@t.text title="SEO关键字" id="seo_keys" value="${bean.seo_keys!''}" /> 
								<@t.text title="SEO描述" id="seo_desc" value="${bean.seo_desc!''}" /> 
								<@t.text title="SEO标题" id="seo_title" value="${bean.seo_title!''}" /> 
                            </@t.form>
                        </div>
                    </div>
</@t.container>
<div id="dd"></div>
       <SCRIPT type="text/javascript">
		//创建子栏目，清空表单，设置PID
		function addChild(){
			//var treeObj = $.fn.zTree.getZTreeObj("tree");
			var nodes = ztreeObj.getSelectedNodes();
			var pid=0;
			if(nodes.length>=1){
				pid=nodes[0].id;
			}
			clearForm('add_form');
			$('#pid').val(pid);
			$('#id').html('');
		}
		function changebtsState(){
			if(!selectId){
				$('#category_toolbar > a,#category_toolbar > button').attr('disabled','true');
				$('#addTopBtn').removeAttr('disabled');
			}else{
				$('#category_toolbar > a,#category_toolbar > button').removeAttr('disabled');
			}
		}
		var selectId=false;       	
		var ztreeObj;
		var setting = {
			view: {
				selectedMulti: false,
				dblClickExpand: false
			},
			data: {
				key:{
    				name:'text'
    			},
				simpleData: {
					enable: true,
					idKey:'id',
					pIdKey:'pid',
				}
			},
			callback: {
					onClick: zTreeOnClick
				}
		};
		
		var zNodes=${cate_tree};
		var timer;
		function deleteCategory() {
			if(!selectId){
				simAlert("没有选择任何栏目!");return;
			}
			var data='ids='+selectId;
			var d=dialog({
			    title: '确认提示',
			    content: "确定删除栏目么？如果删除，子栏目也会删除！",
			    okValue: '确定',
			    ok: function () {
			    	$ .axsf('admin/category/delete',data,function(d){
						if(d.code==200){
							var nodes = ztreeObj.getNodesByParam("id", selectId, null);
							ztreeObj.removeNode(nodes[0]);
						}
					});
			    	return true;
			    },
			    cancelValue: '关闭',
			    cancel: true //为true等价于function(){}
			}).show();
			return false;
		}
		/****点击事件***/
		function zTreeOnClick(event, treeId, treeNode) {
			selectId=treeNode.id;
			changebtsState();
			//查询节点内容
			//延迟触发，防止连续点击造成资源浪费
			if(timer)clearTimeout(timer);
			timer=setTimeout(function(){
				var data='id='+treeNode.id;
    			$ .axsf('admin/category/toAddJson',data,function(d){
    				if(d){
    					 //填充数据
        				for (var key in d) 
                        {
                        	if(key=="id"){$('#id').html(d[key]);}
        					$('#'+key).val(d[key]);
                        }
    				}
    			});
			},300);
		}
		/******************************/
		$(document).ready(function(){
			ztreeObj=$ .fn.zTree.init($("#cate_tree"), setting, zNodes);
			ztreeObj.expandAll(true);
			changebtsState();
			//绑定表单
			/**************提交表单*******************/
    		$('#add_form').validator({
            		theme: 'yellow_right_effect',  //设置theme
            		stopOnError: true, //
            	    fields: {
    					"name": {
            	            rule: "required",
            	            ok: "",
            	            tip:"请输入分类名称!",
            	            msg: {
            	            	required: "分类名称不可以为空!"
            	            }
            	        },
            	        "folder": {
            	            rule: "required",
            	            ok: "",
            	            tip:"请选择或者输入目录!",
            	            msg: {
            	            	required: "目录不可以为空!"
            	            }
            	        }
            	    }
            	});
    		  $('#add_form').on('valid.form', function(e, form){
			  	   //验证type
				   //成功之后刷新gd，关闭自己
				   var add_flag=$('#id').val()==""?true:false;
                   $ .axsf('admin/category/add',$(this).serialize(),function(d){
    			   		if(d){
                             if(d.code==200){
                             	 //填充数据
							 	//修改树节点名称
							 	if(add_flag){
							 		var nodes = ztreeObj.getSelectedNodes();
								 	var obj=$.parseJSON(d.message);
								 	var id=obj.id;
								 	var name=obj.name;
								 	var newNode = {id:id,text:name};
									newNode = ztreeObj.addNodes(nodes[0], newNode);
									ztreeObj.selectNode(newNode);
									simAlert('添加栏目成功！');
							 	}else{
							 		simAlert('修改栏目成功！');
							 	}
                             }else{
								simAlert(d.message);
                             }
                    	}
    			   });
			});
        });
	</SCRIPT>
<script type="text/javascript" src="public/static/ztree/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="public/static/ztree/js/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript" src="public/static/ztree/js/jquery.ztree.exedit-3.5.js"></script>
</@t.html>